package com.wardrobe.soa.api.address;

import com.wardrobe.dao.address.AddressDao;
import com.wardrobe.domain.address.AddressBean;
import com.wardrobe.soa.common.ApiResult;
import com.wardrobe.soa.utils.CommonConsant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/14.
 */
@Service
public class ApiAddressService {
    private static final Logger logger = LoggerFactory.getLogger(ApiAddressService.class);

    @Autowired
    private AddressDao addressDao;

    public ApiResult getAddressList(long userId) {
        try {
            List<AddressBean> addressBeans = addressDao.selectAddressByUserId(userId);
            return new ApiResult(CommonConsant.successCode, CommonConsant.success, CommonConsant.successMsg, addressBeans);
        } catch (Exception e) {
            logger.error("getAddressList error", e);
        }
        return new ApiResult(CommonConsant.serverFailedCode, CommonConsant.failed, CommonConsant.serverFailedMsg, "");
    }

    public ApiResult getDefaultAddress(long userId) {
        try {
            AddressBean addressBean = addressDao.selectDefaultAddress(userId);
            return new ApiResult(CommonConsant.successCode, CommonConsant.success, CommonConsant.successMsg, addressBean);
        } catch (Exception e) {
            logger.error("getDefaultAddress error", e);
        }
        return new ApiResult(CommonConsant.serverFailedCode, CommonConsant.failed, CommonConsant.serverFailedMsg, "");
    }

    public ApiResult modifyDefaultAddress(AddressBean addressBean) {
        try {

            if (addressBean.getIsDefault()==1){
                AddressBean addressBean1 = addressDao.selectDefaultAddress(addressBean.getUserId());
                if (addressBean1!=null){
                    addressBean1.setIsDefault(0);
                    addressDao.updateAddress(addressBean1);
                }
            }
            int flag = addressDao.updateAddress(addressBean);
            if (flag == 1) {
                return new ApiResult(CommonConsant.successCode, CommonConsant.success, CommonConsant.successMsg, flag);
            }
        } catch (Exception e) {
            logger.error("modifyDefaultAddress error", e);
        }
        return new ApiResult(CommonConsant.serverFailedCode, CommonConsant.failed, CommonConsant.serverFailedMsg, "");
    }

    public ApiResult saveAddress(AddressBean addressBean) {
        try {
            if(addressBean.getIsDefault()==1){
                AddressBean addressBean1 = addressDao.selectDefaultAddress(addressBean.getUserId());
                if (addressBean1!=null){
                    addressBean1.setIsDefault(0);
                    addressDao.updateAddress(addressBean1);
                }
            }
            int flag = addressDao.insertAddress(addressBean);
            if (flag == 1) {
                return new ApiResult(CommonConsant.successCode, CommonConsant.success, CommonConsant.successMsg, flag);
            }
        } catch (Exception e) {
            logger.error("saveAddress error", e);
        }
        return new ApiResult(CommonConsant.serverFailedCode, CommonConsant.failed, CommonConsant.serverFailedMsg, "");
    }

    public ApiResult deleteAddress(long id) {
        try {
            int flag = addressDao.deleteAddress(id);
            if (flag==1){
                return new ApiResult(CommonConsant.successCode, CommonConsant.success, CommonConsant.successMsg, flag);
            }
        } catch (Exception e) {
            logger.error("deleteAddress error", e);
        }
        return new ApiResult(CommonConsant.serverFailedCode, CommonConsant.failed, CommonConsant.serverFailedMsg, "");
    }
}
